import {_decorator, Component, game, PhysicsSystem, profiler, sys} from 'cc';
import {Constants} from "db://assets/scripts/common/Constants";
import {StorageManager} from "db://assets/scripts/common/StorageManager";
import {StorageKey} from "db://assets/scripts/common/StorageKey";

const {ccclass, property} = _decorator;

/**
 * 挂载到fight场景下的canvas节点
 */
@ccclass('FightController')
export class FightController extends Component {

    start() {
        // 初始化
        this.init();

        // 引导
        // GuideManager.instance.start();

        // 加载子包
        // SubPackageManager.instance.loadAllPackage();
    }

    /**
     * 初始化
     */
    private init() {
        // 初始化游戏帧率
        // this.initFrameRate();
        // 初始化是否打开调试模式
        this.initDebug();
        // 初始化Cocos数据统计
        this.intiCocosAnalytics();
    }

    /**
     * 初始化游戏帧率
     */
    private initFrameRate() {
        let frameRate = Number(StorageManager.getInstance().getData(StorageKey.FRAME_RATE));
        if (typeof frameRate !== "number") {
            frameRate = Constants.GAME_FRAME;
            //@ts-ignore
            if (window.wx && Util.checkIsLowPhone()) {
                frameRate = 30;
            }

            StorageManager.getInstance().setData(StorageKey.FRAME_RATE, frameRate.toString());
        }

        console.log("###frameRate", frameRate);

        game.frameRate = frameRate;
        PhysicsSystem.instance.fixedTimeStep = 1 / frameRate;
    }

    /**
     * 初始化调试模式
     */
    private initDebug() {
        let isDebugOpen = Boolean(StorageManager.getInstance().getData(StorageKey.DEBUG));
        isDebugOpen === true ? profiler.showStats() : profiler.hideStats();
    }

    /**
     * 初始化Cocos数据统计
     */
    private intiCocosAnalytics() {
        //@ts-ignore
        if (window.cocosAnalytics) {
            //@ts-ignore
            window.cocosAnalytics.init({
                // 游戏ID
                appID: "605630324",
                // 游戏/应用版本号
                version: '1.0.0',
                // 分发渠道
                storeID: sys.platform.toString(),
                // 游戏引擎
                engine: "cocos",
            });
        }
    }
}